//
//  PTTabBarView.m
//  platformDemo
//
//  Created by Owen on 2020/6/9.
//  Copyright © 2020 Owen. All rights reserved.
//

#import "PTTabBarView.h"


@interface PTTabBarView ()

@property (nonatomic,strong) NSMutableArray *btnArray;

@end

@implementation PTTabBarView

- (instancetype)init{
    
    self = [super init];
    
    if (self) {
        
        for (int i = 0; i < 3; i++) {
            
            UIButton *btn = [[UIButton alloc] init];
            btn.frame = CGRectMake(WIDTH/7.0*(2*i + 1), 0, WIDTH/7.0, UITabBarHeight);
            [btn setBackgroundColor:[UIColor blackColor]];
            btn.tag = i + 1;
            [self addSubview:btn];
            [btn addTarget:self action:@selector(touchEvent:) forControlEvents:UIControlEventTouchUpInside];
            [self.btnArray addObject:btn];
        
            
        }
        
        
    }
    
    return self;
}

- (void)setCurrentIndex:(int)currentIndex{
    
    _currentIndex = currentIndex;
    
    [self changeBtnState:_currentIndex];
    
    
}


- (void) changeBtnState:(int) currentIndex{
    
    
    for (int i = 0; i < self.btnArray.count; i++) {
        
        UIButton *btn = self.btnArray[i];
        
        if (btn.tag == currentIndex) {
            
            btn.selected = YES;
        }else{
            
            btn.selected = NO;
        }
    }
    
}

- (void) touchEvent:(UIButton *) btn{
    
    [self changeBtnState:(int)btn.tag];
    [self.delegate touchSelectIndex:(int)btn.tag];
}

@end
